package service

import (
	"github.com/gin-gonic/gin"
	//"github.com/asaskevich/govalidator"
	//"github.com/gorilla/websocket"
	"go_part/models"
	"go_part/models/position"
	"go_part/models/fly"
	"go_part/models/robot"
	"strconv"
	//"fmt"
	//"math/rand"
	//"go_part/utils"
	//"net/http"
	//"time"
  )

// GePositionDeviceList
// @Tags         获得点位设备列表
// @Success      200  {string}  json{"code","message"}
// @Router       /device/GePositionDeviceList [get]
func GePositionDeviceList(c *gin.Context) {

	data := make([]*position.PositionDevice,10)
	data = position.GePositionDeviceList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreatePositionDevice
// @Tags         创建点位设备
// @Param name query string false "点位设备名"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param DeviceTypeID query string false "设备类型ID"
// @Param Longitude query string false "经度"
// @Param Latitude query string false "纬度"
// @Param LevelLocalID query string false "区域ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/CreatePositionDevice [get]
func CreatePositionDevice(c *gin.Context) {

	user := position.PositionDevice{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.DeviceTypeID = c.Query("DeviceTypeID")
	user.Longitude = c.Query("Longitude")
	user.Latitude = c.Query("Latitude")
	user.LevelLocalID = c.Query("LevelLocalID")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindLevelLocalByIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "区域名不存在",
		  })
		return
	}

	user.DeviceLocalID = strconv.Itoa(generateRandomNumber());
	position.CreatePositionDevice(user)
	c.JSON(200 ,gin.H{
		"message": "新增设备成功！",
	  })
}

// FindPositionDeviceByTypeIDAndCompanyID
// @Tags         查找点位设备（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/FindPositionDeviceByTypeIDAndCompanyID [get]
func FindPositionDeviceByTypeIDAndCompanyID(c *gin.Context) {
 
	user := position.PositionDevice{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*position.PositionDevice,100)
	data = position.FindPositionDeviceByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeletePositionDevice
// @Tags         删除点位设备
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/DeletePositionDevice [get]
func DeletePositionDevice(c *gin.Context) {

	user := position.PositionDevice{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	position.DeletePositionDevice(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除设备成功！",
	  })
}

// UpdatePositionDevice
// @Tags         更新点位设备
// @Param id formData string false "id"
// @Param name formData string false "设备名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param DeviceTypeID formData string false "设备类型ID"
// @Param Longitude formData string false "经度"
// @Param Latitude formData string false "纬度"
// @Param VideoID formData string false "视频ID"
// @Param VideoType formData string false "视频类型"
// @Param FromCompany formData string false "供应商"
// @Param FromBrand formData string false "设备品牌"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/UpdatePositionDevice [post]
func UpdatePositionDevice(c *gin.Context) {

	user := position.PositionDevice{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.DeviceTypeID = c.PostForm("DeviceTypeID")
	user.Longitude = c.PostForm("Longitude")
	user.Latitude = c.PostForm("Latitude")
	user.VideoID = c.PostForm("VideoID")
	user.VideoType = c.PostForm("VideoType")
	user.FromCompany = c.PostForm("FromCompany")
	user.FromBrand = c.PostForm("FromBrand")


	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	position.UpdatePositionDevice(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新区域成功！",
	  })
}

// GetRobotDeviceList
// @Tags         获得位点机器人列表
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/GetRobotDeviceList [get]
func GetRobotDeviceList(c *gin.Context) {

	data := make([]*rebot.RobotDevice,10)
	data = rebot.GetRobotDeviceList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateRobotDevice
// @Tags         创建机器人设备
// @Param name query string false "设备名称"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param DeviceTypeID query string false "设备类型ID"
// @Param Longitude query string false "经度"
// @Param Latitude query string false "纬度"
// @Param LevelLocalID query string false "区域ID"
// @Param PosistionID query string false "点区ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/CreateRobotDevice [get]
func CreateRobotDevice(c *gin.Context) {

	user := rebot.RobotDevice{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.DeviceTypeID = c.Query("DeviceTypeID")
	user.Longitude = c.Query("Longitude")
	user.Latitude = c.Query("Latitude")
	user.LevelLocalID = c.Query("LevelLocalID")
	user.PosistionID = c.Query("PosistionID")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindLevelLocalByIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "区域名不存在",
		  })
		return
	}

	user.DeviceLocalID = strconv.Itoa(generateRandomNumber());
	rebot.CreateRobotDevice(user)
	c.JSON(200 ,gin.H{
		"message": "新增机器人成功！",
	  })
}

// FindRobotDeviceByTypeIDAndCompanyID
// @Tags         查找机器人设备（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/FindRobotDeviceByTypeIDAndCompanyID [get]
func FindRobotDeviceByTypeIDAndCompanyID(c *gin.Context) {
 
	user := rebot.RobotDevice{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*rebot.RobotDevice,100)
	data = rebot.FindRobotDeviceByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteRobotDevice
// @Tags         删除机器人设备
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/DeleteRobotDevice [get]
func DeleteRobotDevice(c *gin.Context) {

	user := rebot.RobotDevice{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	rebot.DeleteRobotDevice(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除机器人成功！",
	  })
}

// UpdateRobotDevice
// @Tags         更新机器人设备
// @Param id formData string false "id"
// @Param name formData string false "设备名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param DeviceTypeID formData string false "设备类型ID"
// @Param Longitude formData string false "经度"
// @Param Latitude formData string false "纬度"
// @Param VideoID formData string false "视频ID"
// @Param VideoType formData string false "视频类型"
// @Param FromCompany formData string false "供应商"
// @Param FromBrand formData string false "设备品牌"
// @Param Function formData string false "功能"
// @Param ActionLimit formData string false "约束"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/UpdateRobotDevice [post]
func UpdateRobotDevice(c *gin.Context) {

	user := rebot.RobotDevice{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.DeviceTypeID = c.PostForm("DeviceTypeID")
	user.Longitude = c.PostForm("Longitude")
	user.Latitude = c.PostForm("Latitude")
	user.VideoID = c.PostForm("VideoID")
	user.VideoType = c.PostForm("VideoType")
	user.FromCompany = c.PostForm("FromCompany")
	user.FromBrand = c.PostForm("FromBrand")
	user.Function = c.PostForm("Function")
	user.ActionLimit = c.PostForm("ActionLimit")


	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	rebot.UpdateRobotDevice(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新机器人成功！",
	  })
}

// GetFunctionBasicList
// @Tags         获得位点机器人能力集列表
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/GetFunctionBasicList [get]
func GetFunctionBasicList(c *gin.Context) {

	data := make([]*rebot.FunctionBasic,10)
	data = rebot.GetFunctionBasicList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateFunctionBasic
// @Tags         创建功能描述
// @Param name query string false "功能名"
// @Param CompanyID query string false "公司ID"
// @Param Descript query string false "描述"
// @Param LevelLocalID query string false "区域ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/CreateFunctionBasic [get]
func CreateFunctionBasic(c *gin.Context) {

	user := rebot.FunctionBasic{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.Descript = c.Query("Descript")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := rebot.FindFunctionBasicByName(user.Name)
	if data1.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "设备名已存在",
		  })
		return
	}

	user.FunctionID = strconv.Itoa(generateRandomNumber());
	rebot.CreateFunctionBasic(user)
	c.JSON(200 ,gin.H{
		"message": "新增功能描述成功！",
	  })
}

// FindFunctionBasicByTypeIDAndCompanyID
// @Tags         查找机器人功能（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/FindFunctionBasicByTypeIDAndCompanyID [get]
func FindFunctionBasicByTypeIDAndCompanyID(c *gin.Context) {
 
	user := rebot.FunctionBasic{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*rebot.FunctionBasic,100)
	data = rebot.FindFunctionBasicByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}


// DeleteFunctionBasic
// @Tags         删除功能描述
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/DeleteFunctionBasic [get]
func DeleteFunctionBasic(c *gin.Context) {

	user := rebot.FunctionBasic{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	rebot.DeleteFunctionBasic(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除功能描述成功！",
	  })
}

// UpdateFunctionBasic
// @Tags         更新功能描述
// @Param id formData string false "id"
// @Param name formData string false "功能名称"
// @Param CompanyID formData string false "公司ID"
// @Param Descript formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/UpdateFunctionBasic [post]
func UpdateFunctionBasic(c *gin.Context) {

	user := rebot.FunctionBasic{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.Descript = c.PostForm("Descript")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	rebot.UpdateFunctionBasic(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新功能描述成功！",
	  })
}

// GetLimitBasicList
// @Tags         获得位点机器人约束列表
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/GetLimitBasicList [get]
func GetLimitBasicList(c *gin.Context) {

	data := make([]*rebot.LimitBasic,10)
	data = rebot.GetLimitBasicList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateLimitBasic
// @Tags         创建约束描述
// @Param name query string false "约束名"
// @Param CompanyID query string false "公司ID"
// @Param Descript query string false "描述"
// @Param LevelLocalID query string false "区域ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/CreateLimitBasic [get]
func CreateLimitBasic(c *gin.Context) {

	user := rebot.LimitBasic{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.Descript = c.Query("Descript")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := rebot.FindLimitBasicByName(user.Name)
	if data1.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "设备名已存在",
		  })
		return
	}

	user.LimitID = strconv.Itoa(generateRandomNumber());
	rebot.CreateLimitBasic(user)
	c.JSON(200 ,gin.H{
		"message": "新增约束成功！",
	  })
}

// FindLimitBasicByTypeIDAndCompanyID
// @Tags         查找机器人约束（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/FindLimitBasicByTypeIDAndCompanyID [get]
func FindLimitBasicByTypeIDAndCompanyID(c *gin.Context) {
 
	user := rebot.LimitBasic{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*rebot.LimitBasic,100)
	data = rebot.FindLimitBasicByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteLimitBasic
// @Tags         删除约束描述
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/DeleteLimitBasic [get]
func DeleteLimitBasic(c *gin.Context) {

	user := rebot.LimitBasic{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	rebot.DeleteLimitBasic(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除约束成功！",
	  })
}

// UpdateLimitBasic
// @Tags         更新约束描述
// @Param id formData string false "id"
// @Param name formData string false "name"
// @Param CompanyID formData string false "公司ID"
// @Param Descript formData string false "描述"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/UpdateLimitBasic [post]
func UpdateLimitBasic(c *gin.Context) {

	user := rebot.LimitBasic{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.Descript = c.PostForm("Descript")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	rebot.UpdateLimitBasic(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新约束成功！",
	  })
}

// GetFlyDeviceBasicList
// @Tags         获得点位无人机列表
// @Success      200  {string}  json{"code","message"}
// @Router       /device/robot/GetFlyDeviceBasicList [get]
func GetFlyDeviceBasicList(c *gin.Context) {

	data := make([]*fly.FlyDeviceBasic,10)
	data = fly.GetFlyDeviceBasicList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateFlyDeviceBasic
// @Tags         创建无人机设备
// @Param name query string false "设备名称"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param DeviceTypeID query string false "设备类型ID"
// @Param Longitude query string false "经度"
// @Param Latitude query string false "纬度"
// @Param LevelLocalID query string false "区域ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/CreateFlyDeviceBasic [get]
func CreateFlyDeviceBasic(c *gin.Context) {

	user := fly.FlyDeviceBasic{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.DeviceTypeID = c.Query("DeviceTypeID")
	user.Longitude = c.Query("Longitude")
	user.Latitude = c.Query("Latitude")
	user.LevelLocalID = c.Query("LevelLocalID")
	user.AirportID = c.Query("AirportID")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindLevelLocalByIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "区域名不存在",
		  })
		return
	}

	if user.AirportID != "" {
		data3 := fly.FindAirportInfoByDeviceID(user.AirportID)
		if data3.Name == "" {
			c.JSON(-1 ,gin.H{
				"message": "机场不存在",
			  })
			return
		}
	}

	user.DeviceLocalID = strconv.Itoa(generateRandomNumber());
	fly.CreateFlyDeviceBasic(user)
	c.JSON(200 ,gin.H{
		"message": "新增无人机成功！",
	  })
}

// FindFlyDeviceByTypeIDAndCompanyID
// @Tags         查找无人机（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/FindFlyDeviceByTypeIDAndCompanyID [get]
func FindFlyDeviceByTypeIDAndCompanyID(c *gin.Context) {
 
	user := fly.FlyDeviceBasic{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*fly.FlyDeviceBasic,100)
	data = fly.FindFlyDeviceByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteFlyDeviceBasic
// @Tags         删除无人机设备
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/DeleteFlyDeviceBasic [get]
func DeleteFlyDeviceBasic(c *gin.Context) {

	user := fly.FlyDeviceBasic{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	fly.DeleteFlyDeviceBasic(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除无人机成功！",
	  })
}

// UpdateFlyDeviceBasic
// @Tags         更新无人机设备
// @Param id formData string false "id"
// @Param name formData string false "设备名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param DeviceTypeID formData string false "设备类型ID"
// @Param Longitude formData string false "经度"
// @Param Latitude formData string false "纬度"
// @Param VideoID formData string false "视频ID"
// @Param VideoType formData string false "视频类型"
// @Param FromCompany formData string false "供应商"
// @Param FromBrand formData string false "设备品牌"
// @Param AirportID formData string false "机场ID"
// @Param MaxHeight formData string false "最高高度"
// @Param MaxSpeed formData string false "最高速度"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/UpdateFlyDeviceBasic [post]
func UpdateFlyDeviceBasic(c *gin.Context) {

	user := fly.FlyDeviceBasic{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.DeviceTypeID = c.PostForm("DeviceTypeID")
	user.Longitude = c.PostForm("Longitude")
	user.Latitude = c.PostForm("Latitude")
	user.VideoID = c.PostForm("VideoID")
	user.VideoType = c.PostForm("VideoType")
	user.FromCompany = c.PostForm("FromCompany")
	user.FromBrand = c.PostForm("FromBrand")
	user.AirportID = c.PostForm("AirportID")
	user.MaxHeight = c.PostForm("MaxHeight")
	user.MaxSpeed = c.PostForm("MaxSpeed")


	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	if user.AirportID != "" {
		data3 := fly.FindAirportInfoByDeviceID(user.AirportID)
		if data3.Name == "" {
			c.JSON(-1 ,gin.H{
				"message": "机场不存在",
			  })
			return
		}
	}

	fly.UpdateFlyDeviceBasic(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新无人机成功！",
	  })
}

// GetAirportInfoList
// @Tags         获得点位无人机机场列表（支持虚拟机场）
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/GetAirportInfoList [get]
func GetAirportInfoList(c *gin.Context) {

	data := make([]*fly.AirportInfo,10)
	data = fly.GetAirportInfoList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreateAirportInfo
// @Tags         创建机场
// @Param name query string false "机场名称"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param DeviceTypeID query string false "设备类型ID"
// @Param Longitude query string false "经度"
// @Param Latitude query string false "纬度"
// @Param LevelLocalID query string false "区域ID"
// @Param PositionID query string false "点区ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/CreateAirportInfo [get]
func CreateAirportInfo(c *gin.Context) {

	user := fly.AirportInfo{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.DeviceTypeID = c.Query("DeviceTypeID")
	user.Longitude = c.Query("Longitude")
	user.Latitude = c.Query("Latitude")
	user.LevelLocalID = c.Query("LevelLocalID")
	user.PositionID = c.Query("PositionID")
	user.IsLogout = false
	user.IsPrevent = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindLevelLocalByIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "区域名不存在",
		  })
		return
	}

	data2 := fly.FindAirportInfoByName(user.Name)
	if data2.Name != "" {
		c.JSON(-1 ,gin.H{
			"message": "设备名已存在",
		  })
		return
	}

	if user.PositionID != "" {
		data3 := fly.FindPositionInfoByDeviceID(user.PositionID)
		if data3.Name == "" {
			c.JSON(-1 ,gin.H{
				"message": "机场不存在",
			  })
			return
		}
	}

	user.DeviceID = strconv.Itoa(generateRandomNumber());
	fly.CreateAirportInfo(user)
	c.JSON(200 ,gin.H{
		"message": "新增机场成功！",
	  })
}

// FindAirportByTypeIDAndCompanyID
// @Tags         查找机场（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/FindAirportByTypeIDAndCompanyID [get]
func FindAirportByTypeIDAndCompanyID(c *gin.Context) {
 
	user := fly.AirportInfo{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*fly.AirportInfo,100)
	data = fly.FindAirportByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeleteAirportInfo
// @Tags         删除无人机机场
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/DeleteAirportInfo [get]
func DeleteAirportInfo(c *gin.Context) {

	user := fly.AirportInfo{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	fly.DeleteAirportInfo(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除机场成功！",
	  })
}

// UpdateAirportInfo
// @Tags         更新无人机机场
// @Param id formData string false "id"
// @Param name formData string false "机场名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param DeviceTypeID formData string false "设备类型ID"
// @Param Longitude formData string false "经度"
// @Param Latitude formData string false "纬度"
// @Param RealMessageID formData string false "实时数据ID"
// @Param PositionID formData string false "点区ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/UpdateAirportInfo [post]
func UpdateAirportInfo(c *gin.Context) {

	user := fly.AirportInfo{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.DeviceTypeID = c.PostForm("DeviceTypeID")
	user.Longitude = c.PostForm("Longitude")
	user.Latitude = c.PostForm("Latitude")
	user.RealMessageID = c.PostForm("RealMessageID")
	user.PositionID = c.PostForm("PositionID")


	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	if user.PositionID != "" {
		data3 := fly.FindPositionInfoByDeviceID(user.PositionID)
		if data3.Name == "" {
			c.JSON(-1 ,gin.H{
				"message": "点区不存在",
			  })
			return
		}
	}

	fly.UpdateAirportInfo(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新机场成功！",
	  })
}

// GetPositionInfoList
// @Tags         获得点区列表
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/GetPositionInfoList [get]
func GetPositionInfoList(c *gin.Context) {

	data := make([]*fly.PositionInfo,10)
	data = fly.GetPositionInfoList()

	c.JSON(200, gin.H{
		"message": data,
	  })
}

// CreatePositionInfo
// @Tags         创建点区
// @Param name query string false "点区名称"
// @Param CompanyID query string false "公司ID"
// @Param DeviceInfo query string false "描述"
// @Param DeviceTypeID query string false "设备类型ID"
// @Param Longitude query string false "经度"
// @Param Latitude query string false "纬度"
// @Param LevelLocalID query string false "区域ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/CreatePositionInfo [get]
func CreatePositionInfo(c *gin.Context) {

	user := fly.PositionInfo{}
	user.Name = c.Query("name")
	user.CompanyID = c.Query("CompanyID")
	user.DeviceInfo = c.Query("DeviceInfo")
	user.DeviceTypeID = c.Query("DeviceTypeID")
	user.Longitude = c.Query("Longitude")
	user.Latitude = c.Query("Latitude")
	user.LevelLocalID = c.Query("LevelLocalID")
	user.IsLogout = false

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	data1 := models.FindLevelLocalByIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	if data1.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "区域名不存在",
		  })
		return
	}

	user.DeviceID = strconv.Itoa(generateRandomNumber());
	fly.CreatePositionInfo(user)
	c.JSON(200 ,gin.H{
		"message": "新增点区成功！",
	  })
}

// FindPositionInfoByTypeIDAndCompanyID
// @Tags         查找点区（根据层级ID）
// @Param CompanyID query string false "公司ID"
// @Param LevelLocalID query string false "层级自定义ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/FindPositionInfoByTypeIDAndCompanyID [get]
func FindPositionInfoByTypeIDAndCompanyID(c *gin.Context) {
 
	user := fly.PositionInfo{}
	user.CompanyID = c.Query("CompanyID")
	user.LevelLocalID = c.Query("LevelLocalID")

	data := make([]*fly.PositionInfo,100)
	data = fly.FindPositionInfoByTypeIDAndCompanyID(user.LevelLocalID,user.CompanyID)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": data,
	  })
}

// DeletePositionInfo
// @Tags         删除点区
// @Param id query string false "id"
// @Param CompanyID query string false "公司ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/DeletePositionInfo [get]
func DeletePositionInfo(c *gin.Context) {

	user := fly.PositionInfo{}
	id,_ := strconv.Atoi(c.Query("id"))
	user.ID = uint(id)
	user.CompanyID = c.Query("CompanyID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	fly.DeletePositionInfo(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "删除点区成功！",
	  })
}

// UpdatePositionInfo
// @Tags         更新点区
// @Param id formData string false "id"
// @Param name formData string false "点区名称"
// @Param CompanyID formData string false "公司ID"
// @Param DeviceInfo formData string false "描述"
// @Param DeviceTypeID formData string false "设备类型ID"
// @Param Longitude formData string false "经度"
// @Param Latitude formData string false "纬度"
// @Param PreventFrom formData string false "阻飞信息来源"
// @Param PreventID formData string false "阻飞信息ID"
// @Success      200  {string}  json{"code","message"}
// @Router       /device/fly/UpdatePositionInfo [post]
func UpdatePositionInfo(c *gin.Context) {

	user := fly.PositionInfo{}
	id,_ := strconv.Atoi(c.PostForm("id"))
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.CompanyID = c.PostForm("CompanyID")
	user.DeviceInfo = c.PostForm("DeviceInfo")
	user.DeviceTypeID = c.PostForm("DeviceTypeID")
	user.Longitude = c.PostForm("Longitude")
	user.Latitude = c.PostForm("Latitude")
	user.PreventFrom = c.PostForm("PreventFrom")
	user.PreventID = c.PostForm("PreventID")

	data := models.FindCompanyByID(user.CompanyID)
	if data.Name == "" {
		c.JSON(-1 ,gin.H{
			"message": "企业名不存在",
		  })
		return
	}

	fly.UpdatePositionInfo(user)
	c.JSON(200 ,gin.H{
		"code": 0,
		"message": "更新点区成功！",
	  })
}